package hu.blok1.veilingsite.client.gui;

import java.util.ArrayList;
import java.util.List;

import hu.blok1.veilingsite.client.Veilingsite;
import hu.blok1.veilingsite.client.data.AccountServiceAdmin;
import hu.blok1.veilingsite.client.data.AccountServiceAdminAsync;
import hu.blok1.veilingsite.shared.Product;
import hu.blok1.veilingsite.shared.User;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class adminProductStatus {
	private VerticalPanel vp;
	private User user;

	private final AccountServiceAdminAsync accountService = GWT
			.create(AccountServiceAdmin.class);

	public adminProductStatus(VerticalPanel a, Veilingsite vs) {
		vp = a;
		vp.clear();
		user = vs.getUser();
		if (user.isLoggedIn()) {
			Label lb = new Label("Product wijzigen");
			VerticalPanel mainvertpanel = new VerticalPanel();
			VerticalPanel searchvertpanel = new VerticalPanel();
			mainvertpanel.add(lb);
			HorizontalPanel horipanel = new HorizontalPanel();
			VerticalPanel vertpanel1 = new VerticalPanel();
			VerticalPanel vertpanel2 = new VerticalPanel();
			horipanel.add(vertpanel1);
			horipanel.add(vertpanel2);
			mainvertpanel.add(horipanel);

			Label userName = new Label("Product ID");
			final TextBox userNametb = new TextBox();
			vertpanel1.add(userName);
			vertpanel1.add(userNametb);

			Label userStatus = new Label("Nieuwe status");
			final ListBox box = new ListBox();
			box.addItem("Active");
			box.addItem("InActive");
			box.addItem("Blocked");
			box.addItem("Pending");
			vertpanel2.add(userStatus);
			vertpanel2.add(box);

			Button changestatus = new Button("Wijzig status");
			vertpanel2.add(changestatus);
			changestatus.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					final String id = userNametb.getText();
					int i = box.getSelectedIndex();
					final String newstatus = box.getValue(i);

					accountService.ChangeProductStatus(id, newstatus,
							new AsyncCallback<Void>() {

								public void onFailure(Throwable caught) {
									Window.alert("Error with the Connection :D");
								}

								@Override
								public void onSuccess(Void result) {
									Window.alert("status gewijzigd van productID'"
											+ id
											+ "' naar status: "
											+ newstatus);

								}
							});
				}
			});

			Label lb2 = new Label("Controleer huidige status");
			HorizontalPanel horipanel2 = new HorizontalPanel();
			VerticalPanel vertpanel3 = new VerticalPanel();
			vertpanel3.add(lb2);
			horipanel2.add(vertpanel1);
			horipanel2.add(vertpanel2);
			mainvertpanel.add(horipanel2);

			Button checkstatus = new Button("Controleer status");
			vertpanel2.add(checkstatus);
			checkstatus.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					final String id = userNametb.getText();
					accountService.GetProductStatus(id,
							new AsyncCallback<String>() {

								public void onFailure(Throwable caught) {
									Window.alert("Error with the Connection :D");
								}

								@Override
								public void onSuccess(String result) {

									Window.alert("Huidige status is: " + result);

								}
							});
				}
			});

			Button deleteuser = new Button("Delete product");
			vertpanel2.add(deleteuser);
			deleteuser.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					final String id = userNametb.getText();
					accountService.DeleteProduct(id, new AsyncCallback<Void>() {

						public void onFailure(Throwable caught) {
							Window.alert("Error with the Connection :D");
						}

						@Override
						public void onSuccess(Void result) {

							Window.alert("product verwijderd");

						}
					});
				}
			});

			// edit fields
			VerticalPanel editdataright = new VerticalPanel();
			VerticalPanel editdaterightpart1 = new VerticalPanel();
			VerticalPanel editdaterightpart2 = new VerticalPanel();
			HorizontalPanel buttonpanelright = new HorizontalPanel();
			Label editlabel = new Label("Wijzig gegevens");

			editdaterightpart1.add(editlabel);

			final Label editName = new Label("Naam");
			final TextBox editNamebox = new TextBox();

			editdaterightpart1.add(editName);
			editdaterightpart1.add(editNamebox);

			final Label editPrice = new Label("Minimum Prijs");
			final TextBox editPricebox = new TextBox();

			editdaterightpart1.add(editPrice);
			editdaterightpart1.add(editPricebox);

			final Label editDescription = new Label("Omschrijving");
			final TextArea editDescriptionbox = new TextArea();
			editDescriptionbox.setCharacterWidth(40);
			editDescriptionbox.setVisibleLines(10);
			editdaterightpart2.add(editDescription);
			editdaterightpart2.add(editDescriptionbox);

			editdataright.add(editdaterightpart1);
			editdataright.add(editdaterightpart2);

			// edit fields

			final VerticalPanel rightwrap = new VerticalPanel();
			rightwrap.setVisible(false);
			Button editUser = new Button("Wijzig gegevens");
			vertpanel2.add(editUser);
			editUser.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					if (userNametb.getText().equals("")) {
						Window.alert("U heeft niks ingevuld");
					} else {
						accountService.FindSingleProduct(userNametb.getText(),
								new AsyncCallback<Product>() {

									public void onFailure(Throwable caught) {
										Window.alert("Error with the Connection :D");

									}

									public void onSuccess(Product product) {
										editNamebox.setText(product.getName());
										editPricebox.setText(String
												.valueOf(product
														.getMinimumPrize()));
										editDescriptionbox.setText(product
												.getDescription());
										rightwrap.setVisible(true);
									}
								});
					}
				}
			});

			// / search panel start
			Label searchlabel = new Label("Product zoeken");

			HorizontalPanel horipanel3 = new HorizontalPanel();
			HorizontalPanel horipanel4 = new HorizontalPanel();
			searchvertpanel.add(horipanel3);
			searchvertpanel.add(horipanel4);

			// cell table start
			// Create a CellTable.
			final List<Product> productList = new ArrayList<Product>();
			final CellTable<Product> table = new CellTable<Product>();
			table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

			// Add a text column to show the id.
			TextColumn<Product> idColumn = new TextColumn<Product>() {
				@Override
				public String getValue(Product object) {
					return String.valueOf(object.getProductID());
				}
			};
			table.addColumn(idColumn, "ID");

			// Add a text column to show the name.
			TextColumn<Product> nameColumn = new TextColumn<Product>() {
				@Override
				public String getValue(Product object) {
					return object.getName();
				}
			};
			table.addColumn(nameColumn, "Name");

			// Add a text column to show the id.
			TextColumn<Product> MinimumpriceColumn = new TextColumn<Product>() {
				@Override
				public String getValue(Product object) {
					return String.valueOf(object.getMinimumPrize());
				}
			};
			table.addColumn(MinimumpriceColumn, "minPrice");

			// Add a selection model to handle user selection.
			final SingleSelectionModel<Product> selectionModel = new SingleSelectionModel<Product>();
			table.setSelectionModel(selectionModel);
			selectionModel
					.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
						public void onSelectionChange(SelectionChangeEvent event) {
							Product selected = selectionModel
									.getSelectedObject();
							if (selected != null) {
								userNametb.setText(String.valueOf(selected
										.getProductID()));
							}
						}
					});

			// Set the total row count. This isn't strictly necessary, but it
			// affects
			// paging calculations, so its good habit to keep the row count up
			// to
			// date.
			table.setRowCount(productList.size(), true);

			// Push the data into the widget.
			table.setRowData(0, productList);

			// end cell table
			final TextBox searchtb = new TextBox();
			horipanel3.add(searchtb);
			Button finduser = new Button("Zoek product");
			horipanel3.add(finduser);

			finduser.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					accountService.FindMultiProduct(searchtb.getText(),
							new AsyncCallback<List<Product>>() {

								public void onFailure(Throwable caught) {
									Window.alert("Error with the Connection :D");

								}

								public void onSuccess(List<Product> result) {
									if (result.isEmpty()) {
										Window.alert("Geen producten gevonden");
									} else {
										table.setRowData(result);
										System.out.println("redraw done");
									}
								}
							});
				}
			});

			// panel start
			HorizontalPanel mainwrap = new HorizontalPanel();
			VerticalPanel leftwrap = new VerticalPanel();
			leftwrap.add(mainvertpanel);
			leftwrap.add(searchlabel);
			leftwrap.add(searchvertpanel);

			mainwrap.add(leftwrap);
			mainwrap.add(rightwrap);
			searchvertpanel.add(table);

			// edit panel

			// start edit data page

			// buttons//

			Button closeButton = new Button("Sluiten");
			closeButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					rightwrap.setVisible(false);
				}
			});
			buttonpanelright.add(closeButton);

			Button saveButton2 = new Button("Wijzigingen opslaan");
			saveButton2.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					accountService.UpdateProduct(
							Integer.valueOf(userNametb.getText()),
							editNamebox.getText(),
							Integer.valueOf(editPricebox.getText()),
							editDescription.getText(),
							new AsyncCallback<Void>() {
								public void onFailure(Throwable caught) {
									Window.alert("Error with the Connection :D");
								}

								public void onSuccess(Void result) {
									Window.alert("Wijzigingen succesvol opgeslagen");
									editNamebox.setText("");
									editPricebox.setText("");
									editDescriptionbox.setText("");
								}
							});
				}
			});
			buttonpanelright.add(saveButton2);

			// end buttons//

			rightwrap.add(editlabel);
			rightwrap.add(editdataright);
			rightwrap.add(buttonpanelright);
			// end edit data page

			vp.add(mainwrap);

		} else {
			Window.alert("U moet aangemeld zijn om deze pagina te kunnen zien!");
			History.newItem("login");
		}
	}
}